package com.csc531.adapters;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.csc531.DataTypes.Course;
import com.csc531.DataTypes.Task;

/**
 * @author Javier Figueroa
 */
public class CoursesExpandableListAdapter extends ExpandableListAdapter 
{
	//logging
	private static final String TAG = "CLASSES EXP ADAP TAB";
	
	private HashMap<String,ArrayList<Task>> _classesListContents;
	

	public ArrayList<Course> getCourses() 
	{
		return _courses;
	}
	
	public CoursesExpandableListAdapter(Context context) 
	{
		super(context);
		_classesListContents = new HashMap<String,ArrayList<Task>>();
	}
	
	public void addCourse(Course newCourse) 
	{
		_courses.add(0, newCourse);
	}
	
	public void createCourseWithTasksList() 
	{
		for( Course course : _courses ) 
		{	
			String name = course.getName();
			ArrayList<Task> tasks = course.getTasks();	
			_classesListContents.put(name, tasks);
		}
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) 
	{
		Object[] courses = _classesListContents.keySet().toArray();
		return _classesListContents.get(courses[groupPosition]).get(childPosition);
	}

	@Override
	public int getChildrenCount(int groupPosition) 
	{
		Object[] courses =  _classesListContents.keySet().toArray();
		return _classesListContents.get(courses[groupPosition]).size();
	}

	@Override
	public Object getGroup(int groupPosition) 
	{

		Object[] courses =  _classesListContents.keySet().toArray();
		return courses[groupPosition];
	}

	@Override
	public int getGroupCount() 
	{
		return _classesListContents.keySet().size();
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) 
	{
		TextView textView = getGenericView();
        textView.setText(getGroup(groupPosition).toString());
        textView.setClickable(false);
        return textView;		
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) 
	{
		TextView textView = getGenericView();
		Task task = (Task) getChild(groupPosition, childPosition);
		Log.v(TAG,"task date "+task.getDate());
		textView.setClickable(true);
        textView.setText(task.getTitle());
        return textView;		
	}
}
